package com.wwwc.index.web.servlet;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;
import java.net.*;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;

import com.wwwc.index.web.ejb.database.*;
//import com.wwwc.index.web.database.*;
import com.wwwc.util.web.*;

public class SessionBeanDatabaseServlet extends HttpServlet {
    private EJBDatabaseQuery ejbdb;

    public void doGet(HttpServletRequest request,  HttpServletResponse response)
    {
	doPost(request, response);
    }

    public void doPost(HttpServletRequest request,  HttpServletResponse response) 
    {
	try {
	    response.setContentType("text/html");
	    PrintWriter out = response.getWriter();
	    String action   = request.getParameter("action");
	    String query    = request.getParameter("query");
	    
	    if(action == null || query == null) {
		out.println("<HTML><HEAD><TITLE></TITLE></HEAD><BODY><CENTER>");
		out.println("<H1>Excute Your SQL Through A SessionBean</H1>");
		out.println("<FORM action=SessionBeanDatabaseServlet>");
		out.println("<TABLE border=1  cellSpacing=0 cellPadding=2 style='font-size: 10pt'>");
		out.println("<TR><TD><TEXTAREA name=query rows=10 cols=70 cellSpacing=0 cellPadding=1></TEXTAREA></TD></TR>");
		out.println("</TABLE>");
		out.println("<BR><BR><input type=submit name=action value='Submit' style='font-size: 8pt'>");
		out.println("</FORM>");
		out.println("</CENTER></BODY></HTML>");
		return;
	    }
	    else {
		if(query.toUpperCase().startsWith("INSERT")) {
		    action = "insert";
		}
		else if(query.toUpperCase().startsWith("DELETE")) {
		    action = "delete";
		}
		else if(query.toUpperCase().startsWith("SELECT")) {
		    action = "select";
		}
		else {
		    action = "update";
		}
	    }

	    if(action.equals("select")) {
		ArrayList alist = new ArrayList();
		if (ejbdb == null) {
		    ejbdb = getEJBDatabaseQuery();
		}

		alist = ejbdb.select(query);

		out.println("Servlet --(query string)--> SessionBean --> Database<BR><BR>");
		Iterator it = alist.iterator();
		while (it.hasNext()) {
		    out.println("<BR>"+it.next());
		}
		return;
	    }

	    if(action.equals("delete")) {
		if (ejbdb == null) {
		    ejbdb = getEJBDatabaseQuery();
		}
		
		int rs = ejbdb.delete(query);
		
		out.println("<BR>Query="+query);
		out.println("<BR>Result="+rs);
		return;
	    }

	    if(action.equals("insert")) {
		if (ejbdb == null) {
		    ejbdb = getEJBDatabaseQuery();
		}
		
		int rs = ejbdb.insert(query);

		out.println("<BR>Query="+query);
		out.println("<BR>Result="+rs);
		return;
	    }

	    if(action.equals("update")) {
		if (ejbdb == null) {
		    ejbdb = getEJBDatabaseQuery();
		}
		
		int rs = ejbdb.update(query);
		
		out.println("<BR>Query="+query);
		out.println("<BR>Result="+rs);
		return;
	    }

	    return;
	}
	catch(IOException e) {
	    return;
	}
	catch (Exception e) {
	    return;
	}
    }

    private EJBDatabaseQuery getEJBDatabaseQuery() {
	try {
	    InitialContext initial = new InitialContext();
	    Object objref = initial.lookup("java:comp/env/ejb/DatabaseEJB");
	    EJBDatabaseQueryHome dbhome =
		(EJBDatabaseQueryHome)PortableRemoteObject.narrow(objref, EJBDatabaseQueryHome.class);
	    ejbdb = (EJBDatabaseQuery)dbhome.create();
	} 
	catch (RemoteException ex) {
	    return null;
	}
	catch (CreateException ex) {
	    return null;
	} 
	catch (NamingException ex) {
	    return null;
	}
	return ejbdb;
    }
}






